/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.RetryRecord;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class RetryDetailPanel extends Composite {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context;

	private SimplePanel panel;

	private ViewRetryPanel p;

	private String id;

	// private HashMap<String,String> gIds;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			RetryRecord rs = (RetryRecord) res.getResponse();

			if (rs != null) {
				
				Grid table = new Grid(3, 2);
				table.setWidth("100%");
				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				ColumnFormatter cf = table.getColumnFormatter();

				cf.setWidth(0, "25%");
				cf.setWidth(1, "75%");

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");
				ff.setStyleName(2, 0, "tablecol");

				table.setHTML(0, 0, cons.desc());

				table.setHTML(1, 0, cons.retryError());
				table.setHTML(2, 0, cons.retryData());
				String status = null;

				if (rs.getStatus().equals(EntityConstants.STATUS_OK)) {
					status = cons.success();
				} else {
					status = cons.error();
				}
				table.setHTML(0, 1, cons.description(rs.getRetryTime(),
								status));

				if (rs.getErrorTrace() != null) {
					ScrollPanel vp = new ScrollPanel();
					vp.setSize("500px", "150px");
					vp.add(new HTML(rs.getErrorTrace()));

					table.setWidget(1, 1, vp);
				} else {
					table.setHTML(1, 1, "&nbsp");
				}
				if (rs.getRetryData() != null) {
					ScrollPanel vp = new ScrollPanel();
					vp.setSize("500px", "150px");
					vp.add(new HTML(rs.getRetryData()));

					table.setWidget(2, 1, vp);
				} else {
					table.setHTML(2, 1, "&nbsp");
				}

				panel.add(table);
				// panel.add(new HTML("&nbsp;"));

			} else {
				panel.add(new HTML(cons.noRetry()));
			}

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RetryDetailPanel(StatMessages cons, BifangGWTServiceAsync service,
			GwtContext ctx, ViewRetryPanel p1, String id) {
		this.service = service;
		this.cons = cons;
		this.p = p1;
		this.context = ctx;
		this.id = id;
		
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setSpacing(5);
		panel = new SimplePanel();
		panel.setWidth("100%");

		vp.add(panel);

		Button re = new Button(cons.close());
		re.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				p.setTable();

			}
		});

		vp.add(re);
		initWidget(vp);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.stat.GetRetryDetailAction");
		req.setRequest(id);
		service.call(req, cb);
	}

}
